/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.report;

import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import database.IssuingDAO;
import database.ReportDAO;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import model.Issue;
import model.Revenuebyproduct;
import model.Staff;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Trang
 */
@Namespace("/Administration/Report")
public class RevenueReportAction extends ActionSupport {

    private String startDate;
    private String endDate;
    private List<Issue> issues;
    private String date;
    private String title;
    private double totalAmount;
    private String timemode;
    private String displayMode;
    private List<Revenuebyproduct> revenues = new ArrayList<Revenuebyproduct>();
    public ReportDAO reportDAO = new ReportDAO();
    public IssuingDAO issueDAO = new IssuingDAO();

    public String getStartDate() {
        return startDate;
    }

    //@RequiredStringValidator(message = "Start date is required.")
    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    //@RequiredStringValidator(message = "End date is required.")
    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    public List<Issue> getIssues() {
        return issues;
    }

    public void setIssues(List<Issue> issues) {
        this.issues = issues;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public double getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(double totalAmount) {
        this.totalAmount = totalAmount;
    }

    public List<Revenuebyproduct> getRevenues() {
        return revenues;
    }

    public void setRevenues(List<Revenuebyproduct> revenues) {
        this.revenues = revenues;
    }

    public String getTimemode() {
        return timemode;
    }

    public void setTimemode(String timemode) {
        this.timemode = timemode;
    }

    public String getDisplayMode() {
        return displayMode;
    }

    public void setDisplayMode(String displayMode) {
        this.displayMode = displayMode;
    }

    @Action(value = "revenue-report", results = {
        @Result(name = "success", location = "/manageReport/w_revenueReport.jsp")
    })
    public String RevenueBetween() throws ParseException {

        Map session = ActionContext.getContext().getSession();
        Staff staff = (Staff) session.get("staff");
        return SUCCESS;
    }

    @Action(value = "getrevenue", results = {
        @Result(name = "success", location = "/manageReport/w_displayRevenuebydate.jsp")
    })
    public String Revenue() throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        //System.out.println("Revenue"+revenue);
        Map session = ActionContext.getContext().getSession();
//         if(displayMode.equals("Display By Customer"))
//         {
        if ("Current Year".equals(timemode)) {
            //System.out.println("Trang");
            String year = Calendar.getInstance().get(Calendar.YEAR) + "";
            //System.out.println(year);
            System.out.println("year" + year);
            issues = issueDAO.getIssueByDate(year);
            System.out.println("issue" + issues.size());
            title = "Total Revenue";
            date = year;
            session.put("result", 1);

            //date = year;
        }
        //2013-10-23
        else if ("Current Month".equals(timemode)) {
            //System.out.println("Trang");
            int month1 = Calendar.getInstance().get(Calendar.MONTH) + 1;
            String year = Calendar.getInstance().get(Calendar.YEAR) + "";
            String month = month1 + "";
            System.out.println(month);
            issues = issueDAO.getIssueByDate(year + "-" + month + "-");
            title = "Total Revenue";
            date = month + " - " + Calendar.getInstance().get(Calendar.YEAR) + "";
            session.put("result", 1);
            //date = year;
        }
        else if ("Current Day".equals(timemode)) {
            //System.out.println("Trang");
            Date createdate = new Date();
            issues = issueDAO.getIssueByDate(createdate);
            title = "Total Revenue";
            
            date = dateFormat.format(createdate);
            //date = year;
            session.put("result", 1);
        }

        else 
//            if ("Customer Range".equals(timemode)) 
        { 
            if(displayMode.equals("Display By Customer"))
            {
                Date start = dateFormat.parse(startDate);
                Date end = dateFormat.parse(endDate);

                issues = issueDAO.getIssueBetweenDate(start, end);

                date = startDate+" - "+endDate;
                title = "Total Revenue";
                 session.put("result", 1);
            }
            else
            {
                
            Date start = dateFormat.parse(startDate);
            Date end = dateFormat.parse(endDate);

            //System.out.println("Start"+start);
            //System.out.println("Issue"+issueDAO.getIssueByDate(startDate));
            revenues = reportDAO.getRevenueGroupbyProduct(start, end);
            for(Revenuebyproduct item: revenues)
            {
                System.out.println(item.getProduct().getName() + item.getProduct().getCode());
            }
            
            date =  startDate + " - " + endDate;
            title = "Total Revenue";
            session.put("result", 2);
            }
        }
        if (displayMode.equals("Display By Customer")) {
            totalAmount = 0;
            for (Issue item : issues) {
                totalAmount += item.getOrder().getTotalAmount();
            }
        } else {
            totalAmount = 0;
            for (Revenuebyproduct item : revenues) {
                totalAmount += item.getTotalPrice();
            }
        }

        return SUCCESS;
    }
}
